/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.IsfdSectionResult;
import com.comac.ins.isfd.domain.bo.IsfdSectionResultBo;
import com.comac.ins.isfd.domain.bo.query.IsfdSectionResultQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdSectionResultMainVo;
import com.comac.ins.isfd.domain.vo.IsfdSectionResultVo;
import com.comac.ins.isfd.mapper.IsfdSectionResultMapper;
import com.comac.ins.isfd.service.IIsfdSectionResultService;
import lombok.RequiredArgsConstructor;

/**
 * 型材计算结果Service业务层处理
 *
 * @author hxloongs
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class IsfdSectionResultServiceImpl implements IIsfdSectionResultService {

    @Autowired
    private IsfdSectionResultMapper baseMapper;

    /**
     * 查询型材计算结果
     *
     * @param id 主键
     * @return 型材计算结果
     */
    @Override
    public IsfdSectionResultVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询型材计算结果列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 型材计算结果分页列表
     */
    @Override
    public TableDataInfo<IsfdSectionResultVo> queryPageList(IsfdSectionResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdSectionResult> lqw = buildQueryWrapper(bo);
        Page<IsfdSectionResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的型材计算结果列表
     *
     * @param bo 查询条件
     * @return 型材计算结果列表
     */
    @Override
    public List<IsfdSectionResultVo> queryList(IsfdSectionResultBo bo) {
        LambdaQueryWrapper<IsfdSectionResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdSectionResult> buildQueryWrapper(IsfdSectionResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdSectionResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSecId() != null, IsfdSectionResult::getSecId, bo.getSecId());
        lqw.eq(bo.getSecAlgId() != null, IsfdSectionResult::getSecAlgId, bo.getSecAlgId());
        lqw.eq(bo.getE() != null, IsfdSectionResult::getE, bo.getE());
        lqw.eq(bo.getG() != null, IsfdSectionResult::getG, bo.getG());
        lqw.eq(bo.getXc() != null, IsfdSectionResult::getXc, bo.getXc());
        lqw.eq(bo.getA() != null, IsfdSectionResult::getA, bo.getA());
        lqw.eq(bo.getI1() != null, IsfdSectionResult::getI1, bo.getI1());
        lqw.eq(bo.getI2() != null, IsfdSectionResult::getI2, bo.getI2());
        return lqw;
    }

    /**
     * 新增型材计算结果
     *
     * @param bo 型材计算结果
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdSectionResultBo bo) {
        IsfdSectionResult add = MapstructUtils.convert(bo, IsfdSectionResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改型材计算结果
     *
     * @param bo 型材计算结果
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdSectionResultBo bo) {
        IsfdSectionResult update = MapstructUtils.convert(bo, IsfdSectionResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdSectionResult entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除型材计算结果信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public TableDataInfo<IsfdSectionResultMainVo> queryResultPage(IsfdSectionResultQueryBo bo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(bo.getPageNum());
        pageQuery.setPageSize(bo.getPageSize());
        Wrapper<IsfdSectionResult> wrapper = buildQueryWrapper(bo);
        Page<IsfdSectionResultMainVo> result = baseMapper.queryResultPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }


    protected Wrapper<IsfdSectionResult> buildQueryWrapper(IsfdSectionResultQueryBo bo) {
        QueryWrapper<IsfdSectionResult> lqw = Wrappers.query();
        lqw.eq(StringUtils.isNotBlank(bo.getType()) && !IsfdSectionTypeEnum.ALL.getValue().equals(bo.getType()), "s.type", bo.getType())
            .likeRight(StringUtils.isNotBlank(bo.getSerialNumber()), "s.serial_number", bo.getSerialNumber())
            .eq(StringUtils.isNotBlank(bo.getAttributeNumber()), "s.attribute_number", bo.getAttributeNumber())
            .eq(StringUtils.isNotBlank(bo.getMat1Id()), "s.MAT1_ID", bo.getMat1Id())
            .eq(bo.getSecAlgId() != null, "sr.sec_alg_id", bo.getSecAlgId())
            .orderByDesc("sr.create_time");
        return lqw;
    }
}
